/* 
 * File:   Application.cpp
 * Author: Imre Tabur
 * 
 */

#include <signal.h>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include "Application.h"
#include "Version.h"

#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

#ifdef _WIN32
#include "stdafx.h"
#endif

void workerFunc();

namespace CloudyStorage {

    Application::Application() {
        // Ready for write.
    }

    Application::Application(const Application& orig) {
        // Ready for write.
    }

    Application::~Application() {
        // Ready for write.
    }

    int Application::Args(int argc, const char *argv[], const char *env[]) {
        signal(SIGPIPE, SIG_IGN);
        this->commandName = argv[0];
        for (int i = 1; i < argc; i++) {
            std::string tmp = argv[i];
            this->parameters.push_back(tmp);
        }
        if (argc < 2) {
            fprintf(stdout, "%s Version %d.%d\n",
                    argv[0],
                    PROJECT_MODULE_VERSION_MAJOR,
                    PROJECT_MODULE_VERSION_MINOR);
            fprintf(stdout, "Usage: %s number\n", argv[0]);
            return 1;
        }
        double inputValue = atof(argv[1]);
        double outputValue = sqrt(inputValue);
        fprintf(stdout, "The square root of %g is %g\n", inputValue, outputValue);
        return this->Main();
    }

    int Application::Main() {
        std::cout << "main: startup" << std::endl;
        boost::thread workerThread(workerFunc);
        std::cout << "main: waiting for thread" << std::endl;
        workerThread.join();
        std::cout << "main: done" << std::endl;
        this->profile.load();
        return 0;
    }
}

void workerFunc() {
    boost::posix_time::seconds workTime(3);
    std::cout << "Worker: running" << std::endl;
    // Pretend to do something useful...
    boost::this_thread::sleep(workTime);
    std::cout << "Worker: finished" << std::endl;
}

// dllmain.cpp : Defines the entry point for the DLL application.
#ifdef WIN32

BOOL APIENTRY DllMain(HMODULE hModule,
        DWORD ul_reason_for_call,
        LPVOID lpReserved
        ) {
    switch (ul_reason_for_call) {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}
#endif
